package cn.hfuu.spark;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ChineseCharacterFrequency {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\唐凤穎\\Desktop\\novel.txt"; // 指定小说文件路径
        Map<Character, Integer> frequencyMap = new HashMap<>(); // 用于存储每个汉字及其出现次数

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                for (char ch : line.toCharArray()) {
                    // 判断是否为汉字
                    if (Character.toString(ch).matches("[\\u4e00-\\u9fa5]")) {
                        frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 输出每个汉字及其出现次数
        frequencyMap.forEach((ch, count) -> System.out.println(ch + ": " + count));
    }
}